// pages/store/purchase/setmealList/index.js
import {api,util} from '../../../../js/util';
import {comments} from "../../../../js/modules/mall.js";
import authorize from '../../../../js/modules/authorize';
import BasePage from '../../../../js/common/base-page'
import {config} from "../../../../js/modules/gloab";
import { hideTabbar } from '../../../../js/compatible';
var app = getApp();
BasePage({
  pageKey: "",
  /**
   * 页面的初始数据
   */
  data: {
    Currency:'',//货币符号
    NoLoginShowPrice:false,//
    isLogin:true,//判断是否登录
    currentTab:'1000',
    PageIndex:1,
    PageSize:10,
    hasMoreData:true,
    setmealList:[],
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    count:0,
    keywords:'',
    code:'',
    isIphoneX:app.globalData.isIphoneX,
    tabindex:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //初始化底部导航
    app.initTabBar();
    //加载配置
    this.getWholesaleConfig();
    this.getDataList();
  },
  bindTab(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      currentTab:id
    })
    this.getDataList();
  },
  getDataList(){
    var that = this;
    var data = {
      PageIndex:that.data.PageIndex,
      PageSize:that.data.PageSize,
      Status:that.data.currentTab,
      keywords:that.data.keywords
    };
    comments.getPackageList(data,function(content){
      if(content.Success){
        let hasMore = content.Content.count > that.data.PageIndex * that.data.PageSize;
        var data = content.Content.data;
        //当分页大于1时，累加
        if (that.data.PageIndex > 1) {
          data = that.data.setmealList.concat(data);
        }
        that.setData({
          setmealList:data,
          hasMoreData: hasMore,
          count:content.Content.count
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**加载商品类的配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    config.getWholesaleConfigs(fkid, fkflag,"Wholesale",function (model) {
      that.setData({
        NoLoginShowPrice:model.NoLoginShowPrice,//0：建议零售价 1：不显示价格
        PriceDigits: model.PriceDigits || 2, // 价格小数位数
      });
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //隐藏原生底部导航
    hideTabbar()
    var tabindex = app.getTabBarindex();
    this.setData({
      tabindex:tabindex,
      mainColor:api.titleColor||"#e56b14",
      Currency:api.Currency || '￥',
      isLogin:authorize.isLogin('store')
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.PageIndex = 1;
    this.data.setmealList=[];
    this.getDataList();
    wx.stopPullDownRefresh(); 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      var index = this.data.PageIndex;
      this.setData({
        PageIndex:index+1
      },function(){
        this.getDataList();
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //搜索
  onSearch(e){
    // console.log(e.detail,'---==e.detail');
    this.setData({
      keywords:e.detail
    })
  },
  bindSearch(e){
    this.setData({
      keywords:e.detail
    })
    this.getDataList();
  },
  bindScan(){
    var that = this;
    // 只允许从相机扫码
    wx.scanCode({
      //onlyFromCamera: true,
      success: (res) => {
        //that.data.code = res.result;
        console.log(res.result,'=====code')
        that.setData({
          keywords: res.result
        });
        this.getDataList();
      }
    })
  },
})